greedy sortings *1800

Please click on ads to support us..

C++ Code:

//jay Shree Krishna
//har har mahadev

//a%c=b%c
//(b-a)%c=0
//b-a is multiple of c
// The required number of swaps of adjacent elements to sort a permutation is exactly the number of inversions in it
#pragma GCC optimize("O3,unroll-loops","Ofast")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define p pair
#define pll pair<ll,ll>
#define pii pair<int,int>
#define vpll vector<pair<ll,ll>>
#define ull unsigned long long int
#define vll vector<ll>
#define vii vector<int>
#define vcc vector<char>
#define vdd vector<double>
#define vvll vector<vector<ll>>
#define vvii vector<vector<int>>
#define vvcc vector<vector<char>>
#define vvdd vector<vector<double>>
#define rep(i,a,b) for(i=a;i<=b;i++)
#define repr(i,a,b) for(i=a;i>=b;i--)
#define all(v) v.begin() , v.end()
#define rall(v) v.rbegin(),v.rend()
#define ff first
#define ss second
#define pb push_back
#define rev_sort(v) sort(all(v),greater<ll>())
typedef unsigned long long int ulli;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for (auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for (auto &x : a) out << x << ' '; return out;};

template<typename T1, typename T2> ostream& operator<<(ostream& out, const p<T1, T2>& x) {return out << x.ff << ' ' << x.ss;}
template<typename T1, typename T2> istream& operator>>(istream& in, p<T1, T2>& x) {return in >> x.ff >> x.ss;}

long long int mod =  1e9 + 7;

ll powermod(ll a, ll b, ll m)
{
    if (b == 0) return 1;
    ull k = powermod(a, b / 2, m);
    k = k * k;
    k %= m;
    if (b & 1) k = (k * a) % m;
    return k;
}
ll inverse(ll b, ll mod) {
    return powermod(b, mod - 2, mod);
}
// nCr with modulus

// ll factorial(ll n, ll r){
//     if(r==0||r==n) return 1;

//     // dp[n][r]=
//     return (factorial (n-1,r,dp)%mod + factorial (n-1,r-1,dp)%mod)%mod;

// }
ll sqrtf (ll x) {
    ll ans = 0;
    for (ll k = 1LL << 30; k != 0; k /= 2) {
        if ((ans + k) * (ans + k) <= x) {
            ans += k;
        }
    }
    return ans;
}

// ll kadane( vll arr, ll n) {

//     ll max_end = 0;
//     ll mx = LLONG_MIN;

//     for (ll i = 0; i < n; i++) {
//         max_end += arr[i];
//         if (mx < max_end) {

//             mx = max_end;
//         }
//         if (max_end < 0) {

//             max_end = 0;
//         }
//     }
//     return mx;
// }

void bits(ll n, vll&v) {
    ll k = 0;
    while (n > 0) {
        v[k++] = n % 2;
        n /= 2;
    }
}
// Prime test for large numbers

bool isPrime(ull n, int iter = 10)
{
    if (n < 4) return n == 2 || n == 3;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 0; i < iter; i++)
    {
        ull a = 2 + rand() % (n - 3);
        if (powermod(a, n - 1, n) != 1) return false;
    }
    return true;
}
bool isval(int i, int j, int n, int m) {
    if (i < 0 || j < 0 || i >= n || j >= m) return false;
    return true;
}
ll SOP(ll n, ll k, vll &v) {
    ll dp[k + 1][n + 1];
    ll sum = 1, prev_sum = 1;
    memset(dp, 0, sizeof(dp));
    for (ll i = 1; i <= k; i++) {
        prev_sum = sum;
        sum = 0;
        for (int j = 1; j <= (n - i + 1); j++) {
            dp[i][j] = (v[j - 1] * (prev_sum - dp[i - 1][j])) % mod;
            if (dp[i][j] < 0 ) dp[i][j] += mod;
            prev_sum -= (dp[i - 1][j]) % mod;
            if (prev_sum < 0) prev_sum += mod;
            sum += (dp[i][j]) % mod;
            if (sum < 0) sum += mod;
        }
    }
    return sum;
}
// const ll N = 3e5;
// vll prime(N + 1, -1);
// void sieve(ll n)
// {
//     // for (ll i = 0; i <= n; i++) prime[i] = -1;

//     ll m = sqrt(n);
//     vll ans;
//     for (ll p = 2; p <= m; p++)
//     {

//         //
//         if (prime[p] == -1)
//         {

//             for (ll i = p * 2 ; i <= n; i += p)
//                 if (prime[i] == -1) prime[i] = p;

//         }
//     }

//     return  ;

// }
ll findLowerBound(vector<pair<ll, ll> >& arr,
                  pair<ll, ll>& p1)
{
    // Given iterator points to the
    // lower_bound() of given pair
    auto low = upper_bound(arr.begin(), arr.end(), p1);

    return low - arr.begin();
}
// ll maxvec(vll &v) {
//     ll mx  = LLONG_MIN;
//     ll i;
//     rep(i, 0, v.size() - 1) mx = max(v[i], mx);
//     return mx;
// }
// ll minvec(vll &v) {
//     ll mn  = LLONG_MAX;
//     ll i;
//     rep(i, 0, v.size() - 1) mn = min(v[i], mn);
//     return mn;
// }
ll sumvec(vll &v) {
    ll sum = 0;
    ll i;
    rep(i, 0, v.size() - 1) sum += v[i];
    return sum;
}
ll func(ll i, ll j) {
    if (i % j == 0) return i / j;
    return (i / j) + 1;
}
// double dp[1 << 20];
double helper(ll num, ll n, ll j, vvdd prob) {
    ll curr = __builtin_popcount(num);
    ll den = (curr * (curr - 1)) / 2;
    double ans = 0;

    for (ll k = 0; k < n; k++) {
        if ((num & (1 << k)) && k != j) {
            ans += prob[k][j];
        }
    }
    ans = ans / (1.0 * den);
    return ans;
}
ll tot = 0;

typedef pair<ll, ll> pi;
// vector <ll> dijkstra(ll V, vector<vector<pair<ll, ll>>> adj, ll s, vll &par)
// {
//     priority_queue<pi, vector<pi>, greater<pi> > pq;
//     pq.push({0, s});
//     vector<ll>dist(V, LLONG_MAX);
//     // vll par(V,-1);
//     dist[s] = 0;
//     while (!pq.empty()) {
//         ll dis = pq.top().first;
//         ll ind = pq.top().second;
//         pq.pop();
//         for (auto it : adj[ind]) {
//             if (dist[it.ff] > dis + it.ss) {
//                 par[it.ff] = ind ;
//                 dist[it.ff] = dis + it.ss;
//                 pq.push({dist[it.ff], it.ff});
//             }
//         }
//     }
//     return dist;
// }


// void build (ll ind, ll l, ll r, vll &v, vll &seg) {
//     if (l == r) { seg[ind] = (v[l]); return;}
//     ll mid = (l + r) / 2;
//     build(2 * ind + 1, l, mid, v, seg);
//     build(2 * ind + 2, mid + 1, r, v, seg);
//     ll lft = seg[2 * ind + 1], rgt = seg[2 * ind + 2];
//     seg[ind] = min(lft, rgt);
//     return ;
// }
// void update(ll ind, ll low, ll high, ll l, ll r, ll val, vll &seg) {
//     //update previous remaining updates

//     if (high < l || r < low) return;
//     if (low >= l && high <= r) {
//         seg[ind] += val;

//         return ;
//     }
//     ll mid = (low + high) / 2;
//     update(2 * ind + 1, low, mid, l, r, val, seg);
//     update(2 * ind + 2, mid + 1, high, l, r, val, seg);
//     seg[ind] = (seg[2 * ind + 1] + seg[2 * ind + 2]);

// }
// ll query(ll ind, ll low, ll high, ll l, ll r, vll &seg) {
//     // if (lazy[ind] != 0) {
//     //     seg[ind] += lazy[ind];
//     //     if (low != high) {
//     //         lazy[2 * ind + 1] += lazy[ind];
//     //         lazy[2 * ind + 2] += lazy[ind];
//     //     }
//     //     lazy[ind] = 0;
//     // }
//     if ((r < low) || (l > high)) {
//         return INT_MAX;
//     }
//     if (low >= l && high <= r) {
//         return seg[ind];
//     }
//     ll mid = (low + high) / 2;
//     // ll ans = (1LL << 31) - 1;

//     ll lft = query(2 * ind + 1, low, mid, l, r, seg);
//     ll rgt = query(2 * ind + 2, mid + 1, high, l, r, seg);
//     return min(lft, rgt);
// }
// ll query(ll x) {
//     cout << x << endl;
//     cout.flush();
//     ll y;
//     cin >> y;
//     if (y == 0 || y == -2) {
//         exit(0);
//     }
//     return y;
// }
// const ll N = 2 * 1e5 + 10;
// vll fac(N, 1), inv(N, 1);

// void dfs(ll i, vvll&adj, vll &vis) {
//     // dp[i] = w[i];
//     vis[i] = 1;

//     // ans = max(ans, dp[i]);
//     for (auto it : adj[i]) {
//         if (vis[it]) continue;
//         dfs(it, adj, vis);

//     }

// }
bool vowel (char x) {
    if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') return true;
    return false;
}

// const ll N = 2e5 + 10;
// ll fac[N + 1], inv[N + 1];
// ll ncr(ll n, ll r) {
//     if (n < r) return 0;
//     ll ans = (((fac[n] * inv[n - r]) % mod) * inv[r]) % mod;
//     return ans;
// }
void build (ll ind, ll l, ll r, vll &v, vll &seg) {
    if (l == r) { seg[ind] = v[l]; return;}
    ll mid = (l + r) / 2;
    build(2 * ind + 1, l, mid, v, seg);
    build(2 * ind + 2, mid + 1, r, v, seg);
    seg[ind] = max(seg[2 * ind + 1], seg[2 * ind + 2]);
    return ;
}
// ll query(ll ind, ll low, ll high, ll l, ll r, vll &seg) {

//     if (r < low || l > high) {
//         return LLONG_MIN / 2;
//     }
//     if (low >= l && high <= r) {
//         return seg[ind];
//     }
//     ll mid = (low + high) / 2;
//     ll lft = query(2 * ind + 1, low, mid, l, r, seg);
//     ll rgt = query(2 * ind + 2, mid + 1, high, l, r, seg);
//     return max(lft, rgt);
// }

// void update(ll ind, ll low, ll high, ll l, ll r, ll val, vll &seg) {
//     //update previous remaining updates

//     if (high < l || r < low) return;
//     if (low >= l && high <= r) {
//         seg[ind] += val;

//         return ;
//     }
//     ll mid = (low + high) / 2;
//     update(2 * ind + 1, low, mid, l, r, val, seg);
//     update(2 * ind + 2, mid + 1, high, l, r, val, seg);
//     seg[ind] = (seg[2 * ind + 1] + seg[2 * ind + 2]);

// }
vll help(ll l, ll r, vll curr, vll &v) {

    for (ll i = r; i >= l; i--) curr.pb(v[i]);
    for (ll i = 0; i < l; i++) curr.pb(v[i]);
    return curr;
}

bool helper(ll x) {
    ll y = llround(sqrt(x));
    return y * y == x;
}
int  help(vii &v, ll x) {
    int cnt = 0;
    for (auto it : v) {
        if (helper(it + x)) {
            cnt++;

        }
    }
    return cnt;
}
ll query(vll &v) {
    cout << "? ";
    for (auto it : v) cout << it << " ";
    cout << endl;
    cout.flush();
    ll x;
    cin >> x;
    return x;
}

bool isval(ll i, ll j, ll n, ll m) {
    if ((i < 0) || (j < 0) || (i >= n) || ( j >= m)) return false;
    return true;
}

bool comp(vll &a, vll &b) {
    if (a[1] > b[1]) return true;
    return false;
}
bool comp2(vll &a, vll &b) {
    if (a[1] < b[1]) return true;
    return false;
}
void yahapekrege() {
    ll n;
    cin >> n;
    vvll fir, sec;
    ll i = 0;
    while (n--) {
        ll a, b;
        cin >> a >> b;
        if (a > b) sec.pb({a, b, i});
        else fir.pb({a, b, i});
        i++;
    }
    sort(all(fir), comp);
    sort(all(sec), comp2);
    ll o = 1, s = 1;
    i = 1;
    ll lst = 0;
    vll ao, as;
    if (fir.size()) ao.pb(fir[0][2] + 1);
    if (sec.size()) as.pb(sec[0][2] + 1);
    while (i < fir.size()) {
        if (fir[i][0] < fir[lst][1]) ao.pb(fir[i][2] + 1), lst = i, o++;
        i++;
    }
    i = 1;
    lst = 0;
    while (i < sec.size()) {
        if (sec[i][0] > sec[lst][1]) as.pb(sec[i][2] + 1), lst = i, s++;
        i++;
    }
    cout << max(o, s) << endl;
    if (ao.size() >= as.size()) {
        cout << ao;
    }
    else cout << as;
}
int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    // fac[0] = 1, fac[1] = 1;
    // for (ll i = 2; i <= N; i++) {
    //     fac[i] = (fac[i - 1] * i) % mod;
    // }
    // inv[N] = inverse(N, mod);
    // for (ll i = N - 1; i >= 0; i--) {
    //     inv[i] = inverse(i, mod);
    // }
    // sieve(N);
    // int t;
    // cin >> t;
    // //
    // while (t--) {
    //     // cout<<"Case #"<<a<<": ";

    yahapekrege();
    //     // a++;
    cout << endl;
    // }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols